from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QLabel, QLineEdit, \
    QMessageBox, QPushButton
from PyQt5.QtGui import QIcon
import sys
from src.constant import morse_code as mc, text as t
from src.assets.child_window import ChildWindow
from json import loads as load
from src.assets.asklang import AskWindow

sys = sys
QApplication = QApplication


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        option = open('src/assets/options.json', 'a+')

        option_data = load(option.read())
        lang = option_data['lang']
        if lang is None:
            pass
        else:
            open('')

        self.setWindowIcon(QIcon('assets/morse.ico'))
        self.setWindowTitle('摩斯密码翻译器')

        self.widget = QWidget()
        self.mainLayout = QVBoxLayout()

        self.title = QLabel('———————摩斯电码翻译器———————')
        self.title.setStyleSheet('font-family:"抖音美好体"')
        self.mainLayout.addWidget(self.title)

        self.input = QLineEdit()
        self.input.setStyleSheet('font-family:"抖音美好体"')
        self.input.setPlaceholderText('请输入文本 (英文大写字符)')
        self.mainLayout.addWidget(self.input)

        self.input2 = QLineEdit()
        self.input2.setStyleSheet('font-family:"抖音美好体"')
        self.input2.setPlaceholderText('请输入摩斯密码(使用 . 和 - 表示， / 分隔)')
        self.mainLayout.addWidget(self.input2)

        self.childLayout1 = QHBoxLayout()

        self.button1 = QPushButton('文本转摩斯密码')
        self.button2 = QPushButton('摩斯密码转文本')

        self.button1.setStyleSheet('font-family:"抖音美好体"')
        self.button2.setStyleSheet('font-family:"抖音美好体"')

        self.button1.clicked.connect(self.clicked1)
        self.button2.clicked.connect(self.clicked2)

        self.childLayout1.addWidget(self.button1)
        self.childLayout1.addWidget(self.button2)

        self.mainLayout.addLayout(self.childLayout1)

        self.buttonhelp = QPushButton('帮助')
        self.buttonhelp.setStyleSheet('font-family:"抖音美好体"')
        self.mainLayout.addWidget(self.buttonhelp)
        self.buttonhelp.clicked.connect(self.help)

        self.endtitle = QLabel('—————————————————————')
        self.endtitle.setStyleSheet('font-family:"抖音美好体"')
        self.mainLayout.addWidget(self.endtitle)

        self.widget.setLayout(self.mainLayout)
        self.setCentralWidget(self.widget)

    def get_text(self):
        try:
            a = self.input2.text().split('/')
            text = ''
            for i in range(len(a)):
                text += t[a[i]]
            return text
        except KeyError:
            QMessageBox.critical(self, '错误！', '请正确输入摩斯密码！')
            return
        except IndexError:
            QMessageBox.critical(self, '错误！', '请正确输入摩斯密码！')
            return

    def get_mc(self):
        try:
            a = self.input.text()
            text = ''
            for i in range(len(a)):
                text += mc[a[i]]
                text += '/' if not i == len(a) - 1 else ''
            if text == '':
                QMessageBox.critical(self, '错误！', '请正确输入文本！')
            return text
        except KeyError:
            QMessageBox.critical(self, '错误！', '请正确输入文本！')
            return
        except IndexError:
            QMessageBox.critical(self, '错误！', '请正确输入文本！')
            return

    def clicked1(self):
        self.input2.setText(self.get_mc())

    def clicked2(self):
        self.input.setText(self.get_text())

    def help(self):
        self.child = ChildWindow()
        self.child.show()
